package wy.hanyufang.cn;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.netty.NettyWebServer;
import wy.hanyufang.cn.socket.NettySocketServer;
import wy.hanyufang.cn.socket.handler.SimpleSocketHandler;

/**
 * 扫码登录
 * @author ： wyhanyufang@163.com
 * @time ： 12:39 PM 2021/9/20
 */
@SpringBootApplication
public class ScannerLoginServer {
    public static void main(String[] args) {
        SpringApplication.run(ScannerLoginServer.class,args);
        try{
            new NettySocketServer(9090).start();
        }
        catch (Exception ex){
            ex.printStackTrace();
        }
//        EventLoopGroup eventExecutors = new NioEventLoopGroup();
//        EventLoopGroup childEventExecutors = new NioEventLoopGroup();
//        //添加netty服务
//        try{
//            ServerBootstrap nettyServer = new ServerBootstrap();
//            nettyServer.option(ChannelOption.SO_BACKLOG,1024);
////            nettyServer.option(ChannelOption.SO_TIMEOUT,1000);
//            nettyServer.group(eventExecutors,childEventExecutors)//配置线程池
//                    .channel(NioServerSocketChannel.class)//指定需要处理的channel
//                    .localAddress(9090)//绑定端口
//                    .childHandler(new ChannelInitializer<SocketChannel>() {//客户端连接时操作
//                        @Override
//                        protected void initChannel(SocketChannel socketChannel) throws Exception {
//                            System.out.println("客户端加载...");
//                            socketChannel.pipeline().addLast(new HttpServerCodec());//添加http解码器，websocket是基于http协议的
//                            socketChannel.pipeline().addLast(new ChunkedWriteHandler());//块的方式进行写操作
//                            socketChannel.pipeline().addLast(new HttpObjectAggregator(1024));
//                            socketChannel.pipeline().addLast(new SimpleSocketHandler());
//                            socketChannel.pipeline().addLast(
//                                    new WebSocketServerProtocolHandler("/ws",null,true,65536 * 10));
//                        }
//                    });
//            System.out.println("web socket 任务异步启动...");
//            ChannelFuture sync = nettyServer.bind().sync();//任务异步启动
//            System.out.println("web socket 任务通道关闭..");
//            sync.channel().closeFuture();//关闭服务器通道
//        }
//        catch (Exception ex){
//            ex.printStackTrace();
//        }
//        finally {
//            System.out.println("释放线程池资源...");
//            //释放线程池
//            eventExecutors.shutdownGracefully();
//            childEventExecutors.shutdownGracefully();
//        }
    }
}
